package acm.蓝桥1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * 思路，把河流当作点构建最小生成树
 */
public class 最小生成树加判断连通 {
    static int[] parent;
    static int[] size;
    static int n, m;
    static Graph graph;

    public static void main(String[] args) {
        Reader sc = new Reader();
        n = sc.nextInt();
        m = sc.nextInt();
        parent = new int[n + 2];    //算上河
        size = new int[n + 2];  //算上河
        graph = new Graph(n + 1);
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            graph.insert(a, b, c);
        }
        int k = 0;
        for (int i = 1; i <= n; i++) {
            int v = sc.nextInt();
            if (v != -1) {
                graph.insert(i, n + 1, v);
            }
        }
        for (int i = 1; i < size.length; i++) {
            size[i] = 1;
        }
        //        System.out.println(kruskal());
        System.out.println(kruskal());
    }

    //构建最小生成树，并且构建最小生成树的边。
    private static int kruskal() {
        List<Edge> list = graph.edges;
        list.sort(Comparator.comparingInt(o -> o.weight));
        int res = 0;
        int a = 0;
        for (Edge edge : list) {
            if (union(edge.from, edge.to)) {
                a++;
                res += edge.weight;
            } else if (edge.weight < 0) {
                res += edge.weight;
            }
        }
        //去掉所有码头的情况
        clearUnion();
        int b = 0;
        int k = n + 1;
        int res2 = 0;
        for (Edge edge : list) {
            if (edge.to == graph.map[k]) {
                continue;
            }
            if (union(edge.from, edge.to)) {
                b++;
                res2 += edge.weight;
            } else if (edge.weight < 0) {
                res2 += edge.weight;
            }
        }
        // n要--，因为边比边少一个，而我们的a和b是求边
        if (a!=n){  //建造码头的情况
            res=Integer.MAX_VALUE;
        }
        if (b!=n-1){
            res2=Integer.MAX_VALUE;
        }
        return Math.min(res2, res);
    }

    public static void clearUnion() {
        for (int i = 1; i < parent.length; i++) {
            parent[i] = 0;
            size[i] = 1;
        }
    }

    public static int find(int x) {
        return parent[x] == 0 ? x : (parent[x] = find(parent[x]));
    }

    public static boolean union(Node x, Node y) {
        int rootX = find(x.id);
        int rootY = find(y.id);
        if (rootX == rootY) {
            return false;
        }
        if (size[rootX] > size[rootY]) {
            parent[rootY] = rootX;
            size[rootX] += size[rootY];
        } else {
            parent[rootX] = rootY;
            size[rootY] += size[rootX];
        }
        return true;
    }

    static class Graph {
        Node[] map;
        List<Edge> edges = new ArrayList<>();

        public Graph(int n) {
            map = new Node[n + 1];
            for (int i = 1; i <= n; i++) {
                map[i] = new Node(i);
            }
        }

        public void insert(int from, int to, int w) {
            Edge edge = new Edge(map[from], map[to], w);
            map[from].edges.add(edge);
            map[to].edges.add(edge);
            edges.add(edge);
        }
    }

    static class Node {
        public Node(int id) {
            this.id = id;
        }

        int id;
        List<Edge> edges = new ArrayList<>();
    }

    static class Edge {
        Node from;
        Node to;

        int weight;

        public Edge(Node from, Node to, int weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }
    }

    static
    class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public void clear() {
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
